package net.agasper.unitynotification;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.media.AudioAttributes;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.core.app.NotificationCompat;
import androidx.core.app.TaskStackBuilder;

import com.appsflyer.AppsFlyerProperties;
import com.google.android.exoplayer2.offline.DownloadService;
import com.unity3d.player.UnityPlayer;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class UnityNotificationManager extends BroadcastReceiver {
    private static Set<String> channels = new HashSet();

    public static void CreateChannel(String str, String str2, String str3, int i, String str4, int i2, int i3, int i4, long[] jArr, String str5) {
        if (Build.VERSION.SDK_INT >= 26) {
            channels.add(str);
            NotificationManager notificationManager = (NotificationManager) UnityPlayer.currentActivity.getSystemService("notification");
            NotificationChannel notificationChannel = new NotificationChannel(str, str2, i);
            notificationChannel.setDescription(str3);
            if (str4 != null) {
                Resources resources = UnityPlayer.currentActivity.getResources();
                int identifier = resources.getIdentifier("raw/" + str4, null, UnityPlayer.currentActivity.getPackageName());
                AudioAttributes build = new AudioAttributes.Builder().setUsage(5).setContentType(4).build();
                notificationChannel.setSound(Uri.parse("android.resource://" + str5 + "/" + identifier), build);
            }
            boolean z = false;
            notificationChannel.enableLights(i2 == 1);
            notificationChannel.setLightColor(i3);
            if (i4 == 1) {
                z = true;
            }
            notificationChannel.enableVibration(z);
            if (jArr == null) {
                jArr = new long[]{1000, 1000};
            }
            notificationChannel.setVibrationPattern(jArr);
            notificationManager.createNotificationChannel(notificationChannel);
        }
    }

    private static void createChannelIfNeeded(String str, String str2, String str3, boolean z, boolean z2, String str4) {
        if (!channels.contains(str)) {
            channels.add(str);
            CreateChannel(str, str2, str + " notifications", 3, str3, z ? 1 : 0, -16711936, z2 ? 1 : 0, null, str4);
        }
    }

    public static void SetNotification(int i, long j, String str, String str2, String str3, int i2, String str4, int i3, int i4, String str5, String str6, int i5, String str7, String str8, ArrayList<NotificationAction> arrayList) {
        Activity activity = UnityPlayer.currentActivity;
        AlarmManager alarmManager = (AlarmManager) activity.getSystemService(NotificationCompat.CATEGORY_ALARM);
        Intent intent = new Intent(activity, UnityNotificationManager.class);
        intent.putExtra("ticker", str3);
        intent.putExtra("title", str);
        intent.putExtra("message", str2);
        intent.putExtra("id", i);
        intent.putExtra("color", i5);
        boolean z = true;
        intent.putExtra("sound", i2 == 1);
        intent.putExtra("soundName", str4);
        intent.putExtra("vibrate", i3 == 1);
        if (i4 != 1) {
            z = false;
        }
        intent.putExtra("lights", z);
        intent.putExtra("l_icon", str5);
        intent.putExtra("s_icon", str6);
        intent.putExtra("bundle", str7);
        intent.putExtra(AppsFlyerProperties.CHANNEL, str8);
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("actions", arrayList);
        intent.putExtra("actionsBundle", bundle);
        if (Build.VERSION.SDK_INT >= 23) {
            alarmManager.setExact(0, System.currentTimeMillis() + j, PendingIntent.getBroadcast(activity, i, intent, 134217728));
        } else {
            alarmManager.set(0, System.currentTimeMillis() + j, PendingIntent.getBroadcast(activity, i, intent, 134217728));
        }
    }

    public static void SetRepeatingNotification(int i, long j, String str, String str2, String str3, long j2, int i2, String str4, int i3, int i4, String str5, String str6, int i5, String str7, String str8, ArrayList<NotificationAction> arrayList) {
        Activity activity = UnityPlayer.currentActivity;
        AlarmManager alarmManager = (AlarmManager) activity.getSystemService(NotificationCompat.CATEGORY_ALARM);
        Intent intent = new Intent(activity, UnityNotificationManager.class);
        intent.putExtra("ticker", str3);
        intent.putExtra("title", str);
        intent.putExtra("message", str2);
        intent.putExtra("id", i);
        intent.putExtra("color", i5);
        boolean z = true;
        intent.putExtra("sound", i2 == 1);
        intent.putExtra("soundName", str4);
        intent.putExtra("vibrate", i3 == 1);
        if (i4 != 1) {
            z = false;
        }
        intent.putExtra("lights", z);
        intent.putExtra("l_icon", str5);
        intent.putExtra("s_icon", str6);
        intent.putExtra("bundle", str7);
        intent.putExtra(AppsFlyerProperties.CHANNEL, str8);
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("actions", arrayList);
        intent.putExtra("actionsBundle", bundle);
        alarmManager.setRepeating(0, System.currentTimeMillis() + j, j2, PendingIntent.getBroadcast(activity, i, intent, 0));
    }

    @Override // android.content.BroadcastReceiver
    public void onReceive(Context context, Intent intent) {
        ArrayList arrayList;
        int i;
        String str;
        String str2;
        Resources resources;
        String str3;
        String str4;
        NotificationManager notificationManager = (NotificationManager) context.getSystemService("notification");
        String stringExtra = intent.getStringExtra("ticker");
        String stringExtra2 = intent.getStringExtra("title");
        String stringExtra3 = intent.getStringExtra("message");
        String stringExtra4 = intent.getStringExtra("s_icon");
        String stringExtra5 = intent.getStringExtra("l_icon");
        int intExtra = intent.getIntExtra("color", 0);
        String stringExtra6 = intent.getStringExtra("bundle");
        Boolean valueOf = Boolean.valueOf(intent.getBooleanExtra("sound", false));
        String stringExtra7 = intent.getStringExtra("soundName");
        Boolean valueOf2 = Boolean.valueOf(intent.getBooleanExtra("vibrate", false));
        Boolean valueOf3 = Boolean.valueOf(intent.getBooleanExtra("lights", false));
        int intExtra2 = intent.getIntExtra("id", 0);
        String stringExtra8 = intent.getStringExtra(AppsFlyerProperties.CHANNEL);
        ArrayList parcelableArrayList = intent.getBundleExtra("actionsBundle").getParcelableArrayList("actions");
        Resources resources2 = context.getResources();
        Intent launchIntentForPackage = context.getPackageManager().getLaunchIntentForPackage(stringExtra6);
        TaskStackBuilder.create(context).addNextIntent(launchIntentForPackage);
        PendingIntent activity = PendingIntent.getActivity(context, 0, launchIntentForPackage, 134217728);
        if (stringExtra8 == null) {
            stringExtra8 = "default";
        }
        if (Build.VERSION.SDK_INT >= 26) {
            arrayList = parcelableArrayList;
            str4 = stringExtra8;
            str = stringExtra5;
            resources = resources2;
            i = intExtra2;
            str3 = stringExtra7;
            str2 = stringExtra6;
            createChannelIfNeeded(stringExtra8, stringExtra2, stringExtra7, valueOf3.booleanValue(), valueOf2.booleanValue(), stringExtra6);
        } else {
            arrayList = parcelableArrayList;
            str4 = stringExtra8;
            i = intExtra2;
            str3 = stringExtra7;
            str2 = stringExtra6;
            str = stringExtra5;
            resources = resources2;
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, str4);
        builder.setContentIntent(activity).setAutoCancel(true).setContentTitle(stringExtra2).setContentText(stringExtra3);
        if (Build.VERSION.SDK_INT >= 21) {
            builder.setColor(intExtra);
        }
        if (stringExtra != null && stringExtra.length() > 0) {
            builder.setTicker(stringExtra);
        }
        if (stringExtra4 != null && stringExtra4.length() > 0) {
            builder.setSmallIcon(resources.getIdentifier(stringExtra4, "drawable", context.getPackageName()));
        }
        if (str != null && str.length() > 0) {
            builder.setLargeIcon(BitmapFactory.decodeResource(resources, resources.getIdentifier(str, "drawable", context.getPackageName())));
        }
        if (valueOf.booleanValue()) {
            if (str3 != null) {
                int identifier = resources.getIdentifier("raw/" + str3, null, context.getPackageName());
                builder.setSound(Uri.parse("android.resource://" + str2 + "/" + identifier));
            } else {
                builder.setSound(RingtoneManager.getDefaultUri(2));
            }
        }
        if (valueOf2.booleanValue()) {
            builder.setVibrate(new long[]{1000, 1000});
        }
        if (valueOf3.booleanValue()) {
            builder.setLights(-16711936, 3000, 3000);
        }
        if (arrayList != null) {
            int i2 = 0;
            while (i2 < arrayList.size()) {
                NotificationAction notificationAction = (NotificationAction) arrayList.get(i2);
                builder.addAction((notificationAction.getIcon() == null || notificationAction.getIcon().length() <= 0) ? 0 : resources.getIdentifier(notificationAction.getIcon(), "drawable", context.getPackageName()), notificationAction.getTitle(), buildActionIntent(notificationAction, i2));
                i2++;
                arrayList = arrayList;
            }
        }
        notificationManager.notify(i, builder.build());
    }

    private static PendingIntent buildActionIntent(NotificationAction notificationAction, int i) {
        Activity activity = UnityPlayer.currentActivity;
        Intent intent = new Intent(activity, UnityNotificationActionHandler.class);
        intent.putExtra("id", i);
        intent.putExtra("gameObject", notificationAction.getGameObject());
        intent.putExtra("handlerMethod", notificationAction.getHandlerMethod());
        intent.putExtra("actionId", notificationAction.getIdentifier());
        intent.putExtra(DownloadService.KEY_FOREGROUND, notificationAction.isForeground());
        return PendingIntent.getBroadcast(activity, i, intent, 134217728);
    }

    public static void CancelPendingNotification(int i) {
        Activity activity = UnityPlayer.currentActivity;
        ((AlarmManager) activity.getSystemService(NotificationCompat.CATEGORY_ALARM)).cancel(PendingIntent.getBroadcast(activity, i, new Intent(activity, UnityNotificationManager.class), 134217728));
    }

    public static void ClearShowingNotifications() {
        ((NotificationManager) UnityPlayer.currentActivity.getSystemService("notification")).cancelAll();
    }
}